<?php
/**
 * Class that operate on table 'eventodeportivo'. Database Mysql.
 *
 * @author: http://phpdao.com
 * @date: 2013-11-23 21:12
 */
class EventodeportivoMySqlDAO implements EventodeportivoDAO{

	/**
	 * Get Domain object by primry key
	 *
	 * @param String $id primary key
	 * @return EventodeportivoMySql 
	 */
	public function load($id){
		$sql = 'SELECT * FROM eventodeportivo WHERE idEventoDeportivo = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($id);
		return $this->getRow($sqlQuery);
	}

	/**
	 * Get all records from table
	 */
	public function queryAll(){
		$sql = 'SELECT * FROM eventodeportivo';
		$sqlQuery = new SqlQuery($sql);
		return $this->getList($sqlQuery);
	}
	
	/**
	 * Get all records from table ordered by field
	 *
	 * @param $orderColumn column name
	 */
	public function queryAllOrderBy($orderColumn){
		$sql = 'SELECT * FROM eventodeportivo ORDER BY '.$orderColumn;
		$sqlQuery = new SqlQuery($sql);
		return $this->getList($sqlQuery);
	}
	
	/**
 	 * Delete record from table
 	 * @param eventodeportivo primary key
 	 */
	public function delete($idEventoDeportivo){
		$sql = 'DELETE FROM eventodeportivo WHERE idEventoDeportivo = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($idEventoDeportivo);
		return $this->executeUpdate($sqlQuery);
	}
	
	/**
 	 * Insert record to table
 	 *
 	 * @param EventodeportivoMySql eventodeportivo
 	 */
	public function insert($eventodeportivo){
		$sql = 'INSERT INTO eventodeportivo (nombre_evento, fecha_inicio, fecha_fin, logo_evento, descripcion, estado_evento) VALUES (?, ?, ?, ?, ?, ?)';
		$sqlQuery = new SqlQuery($sql);
		
		$sqlQuery->set($eventodeportivo->nombreEvento);
		$sqlQuery->set($eventodeportivo->fechaInicio);
		$sqlQuery->set($eventodeportivo->fechaFin);
		$sqlQuery->set($eventodeportivo->logoEvento);
		$sqlQuery->set($eventodeportivo->descripcion);
		$sqlQuery->set($eventodeportivo->estadoEvento);

		$id = $this->executeInsert($sqlQuery);	
		$eventodeportivo->idEventoDeportivo = $id;
		return $id;
	}
	
	/**
 	 * Update record in table
 	 *
 	 * @param EventodeportivoMySql eventodeportivo
 	 */
	public function update($eventodeportivo){
		$sql = 'UPDATE eventodeportivo SET nombre_evento = ?, fecha_inicio = ?, fecha_fin = ?, logo_evento = ?, descripcion = ?, estado_evento = ? WHERE idEventoDeportivo = ?';
		$sqlQuery = new SqlQuery($sql);
		
		$sqlQuery->set($eventodeportivo->nombreEvento);
		$sqlQuery->set($eventodeportivo->fechaInicio);
		$sqlQuery->set($eventodeportivo->fechaFin);
		$sqlQuery->set($eventodeportivo->logoEvento);
		$sqlQuery->set($eventodeportivo->descripcion);
		$sqlQuery->set($eventodeportivo->estadoEvento);

		$sqlQuery->setNumber($eventodeportivo->idEventoDeportivo);
		return $this->executeUpdate($sqlQuery);
	}

	/**
 	 * Delete all rows
 	 */
	public function clean(){
		$sql = 'DELETE FROM eventodeportivo';
		$sqlQuery = new SqlQuery($sql);
		return $this->executeUpdate($sqlQuery);
	}

	public function queryByNombreEvento($value){
		$sql = 'SELECT * FROM eventodeportivo WHERE nombre_evento = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}

	public function queryByFechaInicio($value){
		$sql = 'SELECT * FROM eventodeportivo WHERE fecha_inicio = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}

	public function queryByFechaFin($value){
		$sql = 'SELECT * FROM eventodeportivo WHERE fecha_fin = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}

	public function queryByLogoEvento($value){
		$sql = 'SELECT * FROM eventodeportivo WHERE logo_evento = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}

	public function queryByDescripcion($value){
		$sql = 'SELECT * FROM eventodeportivo WHERE descripcion = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}

	public function queryByEstadoEvento($value){
		$sql = 'SELECT * FROM eventodeportivo WHERE estado_evento = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}


	public function deleteByNombreEvento($value){
		$sql = 'DELETE FROM eventodeportivo WHERE nombre_evento = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByFechaInicio($value){
		$sql = 'DELETE FROM eventodeportivo WHERE fecha_inicio = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByFechaFin($value){
		$sql = 'DELETE FROM eventodeportivo WHERE fecha_fin = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByLogoEvento($value){
		$sql = 'DELETE FROM eventodeportivo WHERE logo_evento = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByDescripcion($value){
		$sql = 'DELETE FROM eventodeportivo WHERE descripcion = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByEstadoEvento($value){
		$sql = 'DELETE FROM eventodeportivo WHERE estado_evento = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}


	
	/**
	 * Read row
	 *
	 * @return EventodeportivoMySql 
	 */
	protected function readRow($row){
		$eventodeportivo = new Eventodeportivo();
		
		$eventodeportivo->idEventoDeportivo = $row['idEventoDeportivo'];
		$eventodeportivo->nombreEvento = $row['nombre_evento'];
		$eventodeportivo->fechaInicio = $row['fecha_inicio'];
		$eventodeportivo->fechaFin = $row['fecha_fin'];
		$eventodeportivo->logoEvento = $row['logo_evento'];
		$eventodeportivo->descripcion = $row['descripcion'];
		$eventodeportivo->estadoEvento = $row['estado_evento'];

		return $eventodeportivo;
	}
	
	protected function getList($sqlQuery){
		$tab = QueryExecutor::execute($sqlQuery);
		$ret = array();
		for($i=0;$i<count($tab);$i++){
			$ret[$i] = $this->readRow($tab[$i]);
		}
		return $ret;
	}
	
	/**
	 * Get row
	 *
	 * @return EventodeportivoMySql 
	 */
	protected function getRow($sqlQuery){
		$tab = QueryExecutor::execute($sqlQuery);
		if(count($tab)==0){
			return null;
		}
		return $this->readRow($tab[0]);		
	}
	
	/**
	 * Execute sql query
	 */
	protected function execute($sqlQuery){
		return QueryExecutor::execute($sqlQuery);
	}
	
		
	/**
	 * Execute sql query
	 */
	protected function executeUpdate($sqlQuery){
		return QueryExecutor::executeUpdate($sqlQuery);
	}

	/**
	 * Query for one row and one column
	 */
	protected function querySingleResult($sqlQuery){
		return QueryExecutor::queryForString($sqlQuery);
	}

	/**
	 * Insert row to table
	 */
	protected function executeInsert($sqlQuery){
		return QueryExecutor::executeInsert($sqlQuery);
	}
}
?>